package com.weitu.permission.user.service.impl;

import com.weitu.permission.user.entity.User;
import com.weitu.permission.user.entity.UserDepartment;
import com.weitu.permission.user.entity.UserDuty;
import com.weitu.permission.user.mapper.UserInfoMapper;
import com.weitu.permission.user.mapper.UserMapper;
import com.weitu.permission.user.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author LiMeng 2017/3/13
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final UserInfoMapper userInfoMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, UserInfoMapper userInfoMapper) {
        this.userMapper = userMapper;
        this.userInfoMapper = userInfoMapper;
    }

    @Override
    public User selectById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public User selectByUserName(String name) throws Exception {
        User user=userMapper.selectByUserName(name);
        if (user ==null){
            throw new AuthenticationException();
        }

        return user;
    }

    @Override
    public User selectByName(String name) {
        return userMapper.selectByName(name);
    }

    @Override
    @Transactional
    public int deleteById(String id) {
        return userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    @Transactional
    public int insertSelective(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    @Transactional
    public boolean updateByIdSelective(User user) {
        userInfoMapper.delete(user.getId());
        userInfoMapper.add(user.getUserInfo());
        return userMapper.updateByIdSelective(user) > 0;
    }

    @Override
    public int updatePassword(User user) {
        return userMapper.updatePassword(user);
    }

    @Override
    public List<User> queryList(Map map) {
        return userMapper.queryList(map);
    }

    @Override
    public int queryCount(Map map) {
        return userMapper.queryCount(map);
    }

    @Override
    @Transactional
    public int insertUserDepartment(UserDepartment userDepartment) {
        return userMapper.insertUserDepartment(userDepartment);
    }

    @Override
    @Transactional
    public int deleteUserDepartment(UserDepartment userDepartment) {
        return userMapper.deleteUserDepartment(userDepartment);
    }

    @Override
    @Transactional
    public int deleteUserDutyByUserId(String userId) {
        return userMapper.deleteUserDutyByUserId(userId);
    }

    @Override
    @Transactional
    public int insertUserAndAssociation(User user, UserDepartment userDepartment) {
        //保存用户基本信息
        userInfoMapper.add(user.getUserInfo());
        int count = 0;
        count += userMapper.insertSelective(user);
        count += userMapper.insertUserDepartment(userDepartment);
        return count;
    }

    @Override
    @Transactional
    public int insertUserDuty(UserDuty userDuty) {
        return userMapper.insertUserDuty(userDuty);
    }

    @Override
    @Transactional
    public int insertUserAndAssociation(User user, UserDepartment userDepartment, UserDuty userDuty) {
        //保存用户基本信息
        userInfoMapper.add(user.getUserInfo());

        int count = 0;
        count += userMapper.insertSelective(user);
        count += userMapper.insertUserDepartment(userDepartment);
        count += userMapper.insertUserDuty(userDuty);
        return count;
    }

    @Override
    @Transactional
    public int updateUserDepartment(UserDepartment userDepartment, String newDeptId) {
        int count = 0;
        count += userMapper.deleteUserDepartment(userDepartment);
        userDepartment.setDeptId(newDeptId);
        count += userMapper.insertUserDepartment(userDepartment);
        return count;
    }
    @Override
    public List<User> selectUsersByIds(String[] ids) {
        return this.userMapper.selectUsersByIds(ids);
    }
}
